$(function(){
	dictionaryStart("model","2001");
	dictionaryStart("productionfactory","2002");
	dictionaryStart("ratedcapacity","2003");
	picStart("2004");
	//gtStart(companyId);
	$('#gtId').bind('change',function(){
		fallow();
	});
	$('#picfile').bind('change',function(){
		getPicPath();
	});
	/*$('#deviceName').bind('blur',function(){
		getDeviceName($('#deviceName').val());
	});*/
	$("#model").bind("blur",function(){
		isamorphous();
	});
});

var gtStart=function(companyId){
	$.ajax({
		url : contextPath+"user/zygt/list",
		data : {
			companyId : companyId
		},
		type : 'POST',
		success:function(list){
			//alert(list.length);
			if(null!=list && list.length>0){
				$("#gtId").empty().show();
				var option="";
				for(var i=0;i<list.length;i++){
					option += '<option value="'+list[i].deviceid+'">'+list[i].devicename+'</option>';
				}
				$('#gtId').append(option);
			}
			if(typeof(towercode)== 'undefind'){
				setTimeout(function(){
					fallow();
				},100);
			}else{
				/*setTimeout(function(){
					setDefault('gtId',towercode);
				},100);*/
				setTimeout(function(){
					fallow();
				},300);
			}
		}
	});
};


function fallow(){
	var gtId=$('#gtId').val();
	if(gtId==""){
		alert('请选择中压杆塔');
		return;
	}
	$.ajax({
		url : contextPath+'user/zygt/load',
		data : {
			deviceId : gtId
		},
		type : 'post',
		contentType: "application/x-www-form-urlencoded; charset=utf-8", 
		success : function(data){
			if(data!=null){
				$('#towername').val(data.devicename);
				$('#mainlinecode').val(data.mainlinecode);
				$.ajax({
					url : contextPath+'user/zyxl/load',
					data : {
						deviceId : data.mainlinecode
					},
					type :'post',
					success : function(device){
						$('#mainlinename').val(device.devicename);
						$('#mainlinename1').val(device.devicename);
					}
				});
				$('#linecode').val(data.linecode);
				$.ajax({
					url : contextPath+'user/zyxl/load',
					data : {
						deviceId : data.linecode
					},
					type : 'post',
					success : function(device){
						$('#linename').val(device.devicename);
						$('#linename1').val(device.devicename);
					}
				});
				$('#localcity').val(data.localcity);
				$('#operationunit').val(data.operationunit);
				$('#maintenanceteam').val(data.maintenanceteam);
				$('#voltagelevel').val(data.voltagelevel);
				$('#localcity1').val(data.localcity);
				$('#operationunit1').val(data.operationunit);
				$('#maintenanceteam1').val(data.maintenanceteam);
				$('#voltagelevel1').val(data.voltagelevel);
			}
		}
	});
}

function getPicPath(){
	$('#kzsx_add_pic_text').val($('#picfile').val());
}

function uploadPic(){
	var imgcount=$(".img_p").length+1;
	var pic='picfile';
	var picfile=$('#'+pic).val();
	var deviceName=$('#deviceName').val();
	if(picfile==''){
		alert('请添加设备图片');
		return;
	}
	var photoType=picfile.substring(picfile.lastIndexOf("."),picfile.length).toUpperCase();
	//alert(photoType);
	if(photoType!=".JPG"&&photoType!=".GIF"){
		alert("添加的图片格式错误");
		return;
	}
	if(deviceName==''){
		alert("请输入设备名称");
		return;
	}
	if(imgcount>6){
		alert("添加图片已达极限");
		return;
	}
	changePicWidth();
	var pictype=$('#pictype').val();
	$.ajaxFileUpload({
		/*url : contextPath+"user/picture/upload?devicename="+deviceName+"&pictype="+pictype+"&imgcount="+imgcount,*/
		url : contextPath+"user/picture/upload",
		secureuri : false,
		fileElementId : pic,
		data : {data : [$('#'+pic).attr('name')],
				devicename : deviceName,
				pictype : pictype,
				imgcount : imgcount
		},
		dataType : 'json',
		contentType: "application/x-www-form-urlencoded; charset=utf-8",
		success : function(data,status){
			//alert(data);
			if(data!=null){
				//alert(data.msgDesc);
				//$('#inner_box').css('width',120*imgcount+'px');
				var option="";
				option += '<div class="img_p"><img class="small_pic" id="number'+imgcount+'" src="'+basePath+'upload/'+data.resourcepicname+'" alt="" />';
				option +='<img class="img_del" onclick="imgdel(this)" src="'+basePath+'user/images/add_members_close.png" />';
				//option += '<div class="img_p"><div class="top_div"><img class="small_pic" onload="autoSize(this,98)" alt="" />';
				option += '<p>'+data.msg+'</p></div>';
				$('#clear').before(option);
				//alert(kzsx_img_cnt);
				if(imgcount>3){
					var kzsx_img_left=-120*(imgcount-3);
					$('.kzsx_cont_leftbox .img_flash_box .center .inner_box').stop().animate({'left':kzsx_img_left+'px'},200);
				}
			}
		}
	});
}

function getDeviceName(txt){
	var devicename=$('#deviceName').val();
	if(devicename==""){
		return;
	}
	$.ajax({
		url : contextPath+"user/picture/devicename",
		data : {devicename : txt},
		type : 'post',
		success : function(){
			//alert(txt);
		}
	});
}

function isamorphous(){
	var amorphous=$("#model").val()+"";
	if(amorphous!=""&&amorphous!=null&&amorphous.indexOf('H')>=0){
		$("#isamorphous").val("是");
		$("#isamorphous1").val("是");
	}else{
		$("#isamorphous").val("否");
		$("#isamorphous1").val("否");
	}
}

function add(){
	var devicename=$("#deviceName").val();
	if(devicename==""){
		alert('设备名称为空');
		return;
	}
	if(!validateLon($(":input[name='longitude']").val())){
		alert("经度范围为-180到180");
		return;
	}
	if(!validateLat($(":input[name='latitude']").val())){
		alert("纬度范围为-90到90");
		return;
	}
	/*if($("input[name='localcity']").val()==""){
		alert("所属地市为空");
		return false;
	}
//	if(!isVoltage($("input[name='voltagelevel']").val())){
//		alert("请输入正确格式的电压等级，例：10KV或380V");
//		return;
//	}
	var noloadswitchnumber=$("input[name='noloadswitchnumber']").val();
	if(noloadswitchnumber!="" && !isInt(noloadswitchnumber)){
		alert("无载开关分接档数必须为整数");
		return;
	}
	

	var ratedcapacity=$("#ratedcapacity").val();
	if(ratedcapacity!="" && !isNum(ratedcapacity)){
		alert("额定容量必须为数字");
		return;
	}
	
	var impedancevoltage=$("input[name='impedancevoltage']").val();
	if(impedancevoltage!="" && !isNum(impedancevoltage)){
		alert("阻抗电压必须为数字");
		return;
	}
	
	var noloadcurrent=$("input[name='noloadcurrent']").val();
	if(noloadcurrent!="" && !isNum(noloadcurrent)){
		alert("空载电流必须为数字");
		return;
	}
	
	var shortcircuitloss=$("input[name='shortcircuitloss']").val();
	if(shortcircuitloss!="" && !isNum(shortcircuitloss)){
		alert("短路损耗必须为数字");
		return;
	}
	
	var noloadloss=$("input[name='noloadloss']").val();
	if(noloadloss!="" && !isNum(noloadloss)){
		alert("空载损耗必须为数字");
		return;
	}
	
	var hvratedcurrent=$("input[name='hvratedcurrent']").val();
	if(hvratedcurrent!="" && !isNum(hvratedcurrent)){
		alert("高压侧额定电流必须为数字");
		return;
	}
	
	var lvratedcurrent=$("input[name='lvratedcurrent']").val();
	if(lvratedcurrent!="" && !isNum(lvratedcurrent)){
		alert("低压侧额定电流必须为数字");
		return;
	}
	
	if($("input[name='groundingresistance']").val()!="" && !isNum($("input[name='groundingresistance']").val())){
		alert("接地电阻必须为数字");
		return;
	}
	
	if($("input[name='oilheavy']").val()!="" && !isNum($("input[name='oilheavy']").val())){
		alert("油重必须为数字");
		return;
	}
	
	if($("input[name='groundingresistance']").val()!="" && !isNum($("input[name='groundingresistance']").val())){
		alert("总重必须为数字");
		return;
	}*/
	
	var list=$("#form").serialize();
	$.ajax({
		url : contextPath+"user/zsbyq/add",
		data : {data : list},
		type : "post",
		success : function(data){
			//alert(data.msgDesc);
			//alert($('#inner_box .img_p').length);
			if(data.opResult=="0"){
				if($('#inner_box .img_p').length>0){
					var count=1;
					$('#inner_box .img_p p').each(function(){
						var picname=$(this).text();
						var linecode=$('#linecode').val();
						var resourcepicname=$("#number"+count).attr("src").substring($("#number"+count).attr("src").lastIndexOf("/")+1);
//						alert(resourcepicname);
						var devicecode=data.deviceid;
						var devicename=$('#deviceName').val();
						//alert(picname);
						$.ajax({
							url : contextPath+'user/picture/add',
							type : 'post',
							async: false,
							data : {
								picname : picname,
								resourcepicname : resourcepicname,
								linecode : linecode,
								deviceid : devicecode,
								devicename : devicename,
								devicetype : 2
							},
							success : function(data){
								count++;
							}
						});
					});
				}
				location.href=contextPath+"user/d/zsbyqlist.jsp?t="+new Date().getTime();
			}
		}
	});
}

function edite(){
	var did=$("input[name='deviceid']").val();
	setStatus(did);
	var devicename=$("#deviceName").val();
	if(devicename==""){
		alert('设备名称为空');
		return;
	}
	if(!validateLon($(":input[name='longitude']").val())){
		alert("经度范围为-180到180");
		return;
	}
	if(!validateLat($(":input[name='latitude']").val())){
		alert("纬度范围为-90到90");
		return;
	}
	/*if($("input[name='localcity']").val()==""){
		alert("所属地市为空");
		return false;
	}
//	if(!isVoltage($("input[name='voltagelevel']").val())){
//		alert("请输入正确格式的电压等级，例：10KV或380V");
//		return;
//	}
	var noloadswitchnumber=$("input[name='noloadswitchnumber']").val();
	if(noloadswitchnumber!="" && !isInt(noloadswitchnumber)){
		alert("无载开关分接档数必须为整数");
		return;
	}
	

	var ratedcapacity=$("#ratedcapacity").val();
	if(ratedcapacity!="" && !isNum(ratedcapacity)){
		alert("额定容量必须为数字");
		return;
	}
	
	var impedancevoltage=$("input[name='impedancevoltage']").val();
	if(impedancevoltage!="" && !isNum(impedancevoltage)){
		alert("阻抗电压必须为数字");
		return;
	}
	
	var noloadcurrent=$("input[name='noloadcurrent']").val();
	if(noloadcurrent!="" && !isNum(noloadcurrent)){
		alert("空载电流必须为数字");
		return;
	}
	
	var shortcircuitloss=$("input[name='shortcircuitloss']").val();
	if(shortcircuitloss!="" && !isNum(shortcircuitloss)){
		alert("短路损耗必须为数字");
		return;
	}
	
	var noloadloss=$("input[name='noloadloss']").val();
	if(noloadloss!="" && !isNum(noloadloss)){
		alert("空载损耗必须为数字");
		return;
	}
	
	var hvratedcurrent=$("input[name='hvratedcurrent']").val();
	if(hvratedcurrent!="" && !isNum(hvratedcurrent)){
		alert("高压侧额定电流必须为数字");
		return;
	}
	
	var lvratedcurrent=$("input[name='lvratedcurrent']").val();
	if(lvratedcurrent!="" && !isNum(lvratedcurrent)){
		alert("低压侧额定电流必须为数字");
		return;
	}
	
	if($("input[name='groundingresistance']").val()!="" && !isNum($("input[name='groundingresistance']").val())){
		alert("接地电阻必须为数字");
		return;
	}
	
	if($("input[name='oilheavy']").val()!="" && !isNum($("input[name='oilheavy']").val())){
		alert("油重必须为数字");
		return;
	}
	
	if($("input[name='groundingresistance']").val()!="" && !isNum($("input[name='groundingresistance']").val())){
		alert("总重必须为数字");
		return;
	}*/
	
	var list=$("#form").serialize();
	$.ajax({
		url : contextPath+"user/zsbyq/edite",
		data : {data : list},
		type : "post",
		success : function(data){
			alert(data.msgDesc);
			if(data.opResult=="0"){
				if($('#inner_box .img_p').length>0){
					$('#inner_box .img_p p').each(function(){
						var picname=$(this).text();
						var linecode=$('#linecode').val();
						var devicecode=data.deviceid;
						//alert($(this).parent().children(".small_pic").attr("src").substring($(this).parent().children(".small_pic").attr("src").lastIndexOf("/")+1));
						var resourcepicname=$(this).parent().children(".small_pic").attr("src").substring($(this).parent().children(".small_pic").attr("src").lastIndexOf("/")+1);
						var devicename=$('#deviceName').val();
						var pid=$(this).parent().children(".pid").val();
//						alert(pid);
						if(typeof(pid)=='undefined'){
							pid="";
						}
						//alert(picname);
						$.ajax({
							url : contextPath+'user/picture/edite',
							type : 'post',
							async: false,
							data : {
								picname : picname,
								resourcepicname : resourcepicname,
								linecode : linecode,
								deviceid : devicecode,
								devicename : devicename,
								pid : pid,
								devicetype : 2
							},
							success : function(){
							}
						});
					});
					$.ajax({
						url : contextPath+'user/picture/delete',
						type : 'post',
						async:false,
						data :{
							deviceid : data.deviceid
						},
						success : function(){
							location.href=contextPath+"user/d/zsbyqlist.jsp?t="+new Date().getTime();
						}
					});
				}else{
					$.ajax({
						url : contextPath+'user/picture/delete',
						type : 'post',
						async:false,
						data :{
							deviceid : data.deviceid
						},
						success : function(){
							location.href=contextPath+"user/d/zsbyqlist.jsp?t="+new Date().getTime();
						}
					});
				}
			}
		}
	});
}

function setDefault(elementId,data){
	$('#'+elementId).val(data);
}